package com.coming.mySpring.system.controller;

import com.coming.mySpring.exception.BusinessException;
import com.coming.mySpring.system.service.IShopService;
import com.coming.mySpring.system.domain.Shop;
import com.coming.mySpring.system.query.ShopQuery;
import com.coming.mySpring.utils.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

@RestController
@RequestMapping("/shop")
public class ShopController {
    @Autowired
    public IShopService shopService;


    /**
     * 保存和修改公用的
     * @param shop  传递的实体
     * @return Ajaxresult转换结果
     */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody Shop shop){
        try {
            if(shop.getId()!=null){
                shopService.update(shop);
            }else{
                shopService.save(shop);
            }
            return AjaxResult.success();
        } catch (BusinessException e) {//专业捕获业务异常
            return AjaxResult.error().setMsg(e.getMessage());
        }catch (Exception e){//一般报错就不是业务异常
            e.printStackTrace();
            return AjaxResult.error().setMsg("网络异常,请联系管理员!!!");

        }
    }
    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @DeleteMapping(value="/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            shopService.delete(id);
            return AjaxResult.success();
        } catch (Exception e) {
        e.printStackTrace();
            return AjaxResult.error().setMsg(e.getMessage());
        }
    }
	
    //获取用户
    @GetMapping("/{id}")
    public Shop get(@PathVariable("id")Long id) {
        return shopService.loadOne(id);
    }


    /**
    * 查看所有的员工信息
    * @return
    */
    @GetMapping()
    public List<Shop> list(){
        return shopService.loadAll();
    }


    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping("/list")
    public AjaxResult queryList(@RequestBody ShopQuery query) {
        try {
            List<Shop> pageQuery = shopService.queryList(query);
            Long queryCount = shopService.queryCount(query);
            return AjaxResult.success().setDate(pageQuery).setTotal(queryCount);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return AjaxResult.error();

    }
}
